from typing import List, Dict, Tuple
import numpy as np


def matrix_to_communication_pairs(
        matrix: np.ndarray,
        node_prefix: str = "ToR"
) -> Dict[Tuple[str, str], float]:
    """
    将流量矩阵转换为通信对字典

    参数:
        matrix: N x N 流量矩阵
        node_prefix: 节点名称前缀（如 "ToR"）

    返回:
        {(src, dst): demand} 的字典
    """
    n = matrix.shape[0]
    communication_pairs = {}

    for i in range(n):
        for j in range(n):
            if i == j:
                continue  # 跳过自环
            demand = matrix[i][j]
            if demand > 0:
                src = f"{node_prefix}{i + 1}"  # 索引从0开始，节点编号从1开始
                dst = f"{node_prefix}{j + 1}"
                communication_pairs[(src, dst)] = demand

    return communication_pairs

# 示例流量矩阵
traffic_matrix = np.array([
    [0, 10, 5, 0],
    [8, 0, 0, 12],
    [3, 0, 0, 7],
    [0, 15, 6, 0]
])

# 生成通信对字典
pairs = matrix_to_communication_pairs(traffic_matrix)

# 输出结果
print("通信对及流量需求:")
# for (src, dst), demand in pairs.items():
#     print(f"{src} → {dst}: {demand}GB")
for pair in pairs:
    print(pair)